/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *              C E D A R
 *          S O L U T I O N S       "Software done right."
 *           S O F T W A R E
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Copyright (c) 2011 Kenneth J. Pronovici.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Apache License, Version 2.0.
 * See doc/LICENSE for more information about the licensing terms.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Author   : Kenneth J. Pronovici <pronovic@ieee.org>
 * Language : Java 6
 * Project  : Secret Santa Exchange
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package com.cedarsolutions.santa.client.internal.presenter;

import java.util.Date;

import com.cedarsolutions.client.gwt.event.UnifiedEvent;
import com.cedarsolutions.client.gwt.event.ViewEventHandler;
import com.cedarsolutions.exception.InvalidDataException;
import com.cedarsolutions.santa.client.common.widget.WidgetUtils;
import com.cedarsolutions.santa.client.internal.InternalEventBus;
import com.cedarsolutions.santa.client.internal.view.BugReportDialogView;
import com.cedarsolutions.santa.client.internal.view.IBugReportDialogView;
import com.cedarsolutions.santa.client.rpc.IBugReportRpcAsync;
import com.cedarsolutions.santa.client.rpc.util.AsyncRpcCallback;
import com.cedarsolutions.santa.shared.domain.BugReport;
import com.cedarsolutions.santa.shared.domain.ClientSession;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

/**
 * Presenter for the bug report dialog.
 * @author Kenneth J. Pronovici <pronovic@ieee.org>
 */
@Presenter(view = BugReportDialogView.class)
public class BugReportDialogPresenter extends BasePresenter<IBugReportDialogView, InternalEventBus> {

    /** Client session used by this module. */
    @Inject private ClientSession session;

    /** Bug report service. */
    @Inject private IBugReportRpcAsync bugReportRpc;

    /** Bind the user interface. */
    @Override
    public void bind() {
        this.view.setSubmitEventHandler(new SubmitEventHandler(this));
    }

    /** Show the bug report dialog. */
    public void onShowBugReportDialog() {
        BugReport bugReport = new BugReport();
        bugReport.setReportDate(new Date());
        bugReport.setSubmittingUser(session.getCurrentUser());
        bugReport.setApplicationVersion(WidgetUtils.getInstance().getApplicationVersion());
        this.getView().showDialog(bugReport);
    }

    public ClientSession getSession() {
        return this.session;
    }

    public void setSession(ClientSession session) {
        this.session = session;
    }

    public IBugReportRpcAsync getBugReportRpc() {
        return this.bugReportRpc;
    }

    public void setBugReportRpc(IBugReportRpcAsync bugReportRpc) {
        this.bugReportRpc = bugReportRpc;
    }

    /** Event handler for submit action. */
    protected static class SubmitEventHandler implements ViewEventHandler {
        private BugReportDialogPresenter presenter;

        public SubmitEventHandler(BugReportDialogPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void handleEvent(UnifiedEvent event) {
            BugReport bugReport = this.presenter.getView().getBugReport();
            this.presenter.bugReportRpc.submitBugReport(bugReport, new SubmitBugReportCallback(this.presenter));
        }
    }

    /** Callback for IBugReportRpc.submitBugReport(). */
    protected static class SubmitBugReportCallback extends AsyncRpcCallback<Void> {
        private BugReportDialogPresenter presenter;

        public SubmitBugReportCallback(BugReportDialogPresenter presenter) {
            super("IBugReportRpc", "submitBugReport");
            this.presenter = presenter;
        }

        @Override
        public void onSuccessResult(Void result) {
            this.presenter.getView().hideDialog();
        }

        @Override
        public boolean onValidationError(InvalidDataException caught) {
            this.presenter.getView().showValidationError(caught);
            return true;  // indicate that we've handled the validation error
        }
    }
}
